/**
 * 
 */
package com.wisdytech.linkdcs.system.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.system.dao.ISysResourceDao;
import com.wisdytech.linkdcs.system.dao.ISysRoleDao;
import com.wisdytech.linkdcs.system.dao.ISysRoleResourceDao;
import com.wisdytech.linkdcs.system.dao.ISysUserRoleDao;
import com.wisdytech.linkdcs.system.model.*;
import com.wisdytech.linkdcs.system.service.ISystemRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * ClassName: 
 * </p>
 * <p>
 * Description: 
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-13
 * </p>
 */
@Service("systemRoleService")
@Transactional
public class SystemRoleServiceImpl extends BaseService<ISysRoleDao, SysRole> implements ISystemRoleService {
    
    @Resource
    private ISysRoleDao sysRoleDao;
    
    @Resource
    private ISysResourceDao sysResourceDao;
    
    @Resource
    private ISysUserRoleDao sysUserRoleDao;
    
    @Resource
    private ISysRoleResourceDao sysRoleResourceDao;
    
    @Override
    public StandardResult addSystemRole(SysRole sysRole) {
        EntityWrapper<SysRole> ew = new EntityWrapper<>();
        //验证角色名称的唯一性
        List<SysRole> userRoleList;
        userRoleList = sysRoleDao.selectPage(new Page<SysRole>(1, 1), ew.eq("role_name", sysRole.getRoleName()).eq("del_flag", CommonConstants.DEL_FLAG));
        if (!userRoleList.isEmpty()) {
            return StandardResult.getFailResult(CommonConstants.ROLR_NAME);
        }
        sysRole.setPkId(BaseUtil.getUUID());
        sysRole.setDelFlag(CommonConstants.DEL_FLAG);
        sysRole.setCreateDate(new Date());
        SysUser currentUser = BaseUtil.getCurrentUser();
        sysRole.setCreateUser(currentUser!=null?currentUser.getPkId():"test1");
        int count;
        count = sysRoleDao.insert(sysRole);
        if (count <= 0) {
            throw new ServiceException("新增失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override

    public StandardResult delSystemRoleById(String id) {
        List<String> idList = new ArrayList<String>();
        String[] str = id.split(",");
        for (int i = 0; i < str.length; i++) {
            idList.add(str[i]);
        }
        for (String roleId : idList) {
            List<SysUserRole> userRoleList = new ArrayList<SysUserRole>();
            EntityWrapper<SysUserRole> ewUserRole = new EntityWrapper<>();
            userRoleList = sysUserRoleDao.selectList(ewUserRole.eq("role_id", roleId));
            if (!userRoleList.isEmpty()) {
                return StandardResult.getFailResult(CommonConstants.ROLE_USER);
            }
            EntityWrapper<SysRoleResource> ewResource = new EntityWrapper<>();
            EntityWrapper<SysUserRole> ewRole = new EntityWrapper<>();
            int countResource = sysRoleResourceDao.delete(ewResource.eq("role_id", roleId));
            int countRole = sysUserRoleDao.delete(ewRole.eq("role_id", roleId));
            if (countResource<0 || countRole<0) {
                throw new ServiceException("删除失败");
            }
        }
        int count = sysRoleDao.deleteBatchIds(idList);
        if (count<0) {
            throw new ServiceException("删除失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult updateSystemRole(SysRole sysRole) {
        //验证角色名称的唯一性
        EntityWrapper<SysRole> ew = new EntityWrapper<>();
        List<SysRole> userRoleList;
        userRoleList = sysRoleDao.selectPage(new Page<SysRole>(1, 1), ew.eq("role_name", sysRole.getRoleName()).ne("pk_id", sysRole.getPkId()));
        if (userRoleList.size() > 0){
            return StandardResult.getFailResult(CommonConstants.ROLR_NAME);
        }
        
        int count = sysRoleDao.updateById(sysRole);
        if (count<= 0) {
            throw new ServiceException("更新失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public PageInfo<SysRole> listSysRoles(String roleName, String roleType, Filter filter) {
        EntityWrapper<SysRole> ew =new EntityWrapper<>();
        if (StringUtils.isNotBlank(roleName)) {
            ew.like("role_name", roleName);
        }
        if (StringUtils.isNotBlank(roleType)) {
            ew.like("role_type", roleType);
        }
        ew.orderBy("create_date", false);
        List<SysRole> roleList = super.beforeList(ISysRoleDao.class, filter).selectList(ew.eq("del_flag", CommonConstants.DEL_FLAG));
        //List<SysRole> roleList = super.beforeList(ISysRoleDao.class, filter).listAllRole();
        PageInfo<SysRole> rolePage=new PageInfo<>(roleList);
        return rolePage;
    }

    @Override
    public List<SysRole> listRoleByUserId(String userId) {
        return sysRoleDao.listRoleByUserId(userId);
    }
    
    @Override
    public SysRole getSysRoleById(String id) {
        return sysRoleDao.selectById(id);
    }

    @Override
    public List<SysResource> listSysResourceByRoleId(String roleId) {
        return sysResourceDao.listSysResourceByRoleId(roleId);
    }

    @Override
    public List<SysRole> listAllRole() {
        return sysRoleDao.listAllRole();
    }
    
}
